package com.example.ArrayList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Test3Collection {
    public static void main(String[] args) {
        //method();
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");

        //1,数据类型一定是集合或者数组中元素的类型
        //2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素
        //3,list就是要遍历的集合或者数组，
        for (String str : list) {
            System.out.println(str);
        }

        //method3(list);
        //method2(list);
    }

    private static void method3(ArrayList<String> list) {
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if ("b".equals(s)) {
                //指向谁就删除谁
                it.remove();
            }
        }
        System.out.println(list);   //[a, c, d]
    }

    private static void method2(ArrayList<String> list) {
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if ("b".equals(s)) {
                list.remove(i);
                i--;
            }
        }
        System.out.println(list);
    }

    private static void method() {
        Collection<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}
